$(document).ready(function(){
	$("#btnOk").click(function() {
	     $("#btnOk").attr("disabled", "true");
	     setTimeout(function() {
	       window.location.href = "./"
	     }, 1);
	     // $('#toast').toast("Checked In/Out information submitted");
	    });

	

	function findName(array){
		var name;
		$.each(array, function(index, element){
			if ((element == '1') & (index != "id")){
				name = index;
				
			}
		});
		return name;
	}
	
	function findMultipleNames(array){
		var names = new Array();
		$.each(array, function(index, element){
			if ((element == '1') & (index != "id")){
				names.push(index);				
			}
		});
		return names;
	}
	
	
	function getNameText(name){

		return name.replace(/([a-z])([A-Z])/, '$1 $2');
	}
	
	function getDateText(date){
		var result = new Array();
		result[0] = $.datepicker.formatDate(' M d', date);
		result[1] = 'th, at ';
		if (date.getHours() > 12) {
		    result[2] = date.getHours() - 12;
		} else if (date.getHours() == 0 ) {
		    result[2] = "12";
		} else {
		    result[2] = date.getHours();
		}
		result[3] = ":";
		var minutes = date.getMinutes();
		if (minutes < 10){
			result[4] = "0" + minutes; 
		} else {
		result[4] = minutes;
		}

		if (date.getHours() > 12) {
		    result[5] = " pm";
		} else {
		    result[5] = " am";
		}

		return result.join('');
	}
	
	

	
	
	
	
	$.getJSON('./utilities/log_model.php', {operation: 'get_page_of_entries'}, function(data){
		
		$.getJSON('./utilities/children_model.php', {operation: 'get_all_children'}, function(data2){
			function processEntry(entry, MasterTable){
				var type = entry['type'];
	            var new_entry;
	            var date = new Date(entry.date);
            	var dateText = getDateText(date);
	            
	            if (type == 'check_in'){
//	            	find name
	            	var name = findName(entry);
	            	var nameText = getNameText(name);	            	
	            	var imageText = "." + MasterTable[nameText];
	            	
	            	
	            	new_entry = new ArchiveEntry(nameText, imageText, 'checkin', dateText);
	            	$("#log-list").append(new_entry.getLi());
	            	
	            } else if (type == 'check_out'){
	            	var name = findName(entry);
	            	var nameText = getNameText(name);           	
	            	var imageText = "." + MasterTable[nameText];
	            	
	            	new_entry = new ArchiveEntry(nameText, imageText, 'checkout', dateText);
	            	$("#log-list").append(new_entry.getLi());
	            	
	            } else if (type == 'share_story'){
	            	var names = findMultipleNames(entry);  
	            	
	            	if (names.length == 1){
	            		var nameText = getNameText(names.pop());
	            		var imageText = "." + MasterTable[nameText];
	            		
	            		new_entry = new ArchiveEntry(nameText, imageText, 'story', dateText);
		            	$("#log-list").append(new_entry.getLi());
	            		
	            	} else if (names.length == 2){
	            		//first
	            		var nameText = getNameText(names.pop());
	            		var imageText = "." + MasterTable[nameText];
	            		
	            		new_entry = new ArchiveEntry(nameText, imageText, 'story', dateText);
		            	$("#log-list").append(new_entry.getLi());
		            	
		            	//2nd
		            	var nameText = getNameText(names.pop());  
	            		var imageText = "." + MasterTable[nameText];
	            		
	            		new_entry = new ArchiveEntry(nameText, imageText, 'story', dateText);
		            	$("#log-list").append(new_entry.getLi());
	            		
	            	} else {
	            		var nameText = "Multiple chidren";
	            		var imageText = "./images/multiple.jpg";
	            		new_entry = new ArchiveEntry(nameText, imageText, 'storyMultiple', dateText);
		            	$("#log-list").append(new_entry.getLi());
	            	}
	            	
	            	
	            }else if (type == 'lunch_report'){
	            	var names = findMultipleNames(entry);  
	            	
	            	if (names.length == 1){
	            		var nameText = getNameText(names.pop());
	            		var imageText = "." + MasterTable[nameText];
	            		
	            		new_entry = new ArchiveEntry(nameText, imageText, 'lunch', dateText);
		            	$("#log-list").append(new_entry.getLi());
	            		
	            	} else if (names.length == 2){
	            		//first
	            		var nameText = getNameText(names.pop());
	            		var imageText = "." + MasterTable[nameText];
	            		
	            		new_entry = new ArchiveEntry(nameText, imageText, 'lunch', dateText);
		            	$("#log-list").append(new_entry.getLi());
		            	
		            	//2nd
		            	var nameText = getNameText(names.pop());  
	            		var imageText = "." + MasterTable[nameText];
	            		
	            		new_entry = new ArchiveEntry(nameText, imageText, 'lunch', dateText);
		            	$("#log-list").append(new_entry.getLi());
	            		
	            	} else {
	            		var nameText = "Multiple chidren";
	            		var imageText = "./images/multiple.jpg";
	            		new_entry = new ArchiveEntry(nameText, imageText, 'lunchMultiple', dateText);
		            	$("#log-list").append(new_entry.getLi());
		            	
	            	}
	            	
	            } else if (type == 'daily_log'){
	            	var names = findMultipleNames(entry);  
	            	if (names.length == 1){	            		
	            		var nameText = getNameText(names.pop());
	            		var imageText = "." + MasterTable[nameText];
	            		
	            		new_entry = new ArchiveEntry(nameText, imageText, 'dailyLog', dateText);
		            	$("#log-list").append(new_entry.getLi());
	            		
	            	} else if (names.length == 2){
	            		//first
	            		var nameText = getNameText(names.pop());
	            		var imageText = "." + MasterTable[nameText];
	            		
	            		new_entry = new ArchiveEntry(nameText, imageText, 'dailyLog', dateText);
		            	$("#log-list").append(new_entry.getLi());
		            	
		            	//2nd
		            	var nameText = getNameText(names.pop());  
	            		var imageText = "." + MasterTable[nameText];
	            		
	            		new_entry = new ArchiveEntry(nameText, imageText, 'dailyLog', dateText);
		            	$("#log-list").append(new_entry.getLi());
	            		
	            	} else {
	            		var nameText = "Multiple chidren";
	            		var imageText = "./images/multiple.jpg";
	            		new_entry = new ArchiveEntry(nameText, imageText, 'dailyLogMultiple', dateText);
		            	$("#log-list").append(new_entry.getLi());
	            	}
	            	
	            } else{
	            	console.log(type);
	            }
			}
			//end of process Entry
			function processEntries(entries, MasterTable){
				for (var i = 0; i < entries.length; i++){
		            var entry = entries[i];
		            processEntry(entry, MasterTable);
				}
				
			}
			function redoQuery(MasterTable){
				var childrenQuery = $("#select_children").val();
				   var typeQuery = $("#select_type").val();
				   $.post('./utilities/log_model.php', {operation: 'query_page_by_name_types', 'names[]': childrenQuery, 'types[]': typeQuery}, function(data){
						var temp = data.replaceAll("[", "");
						var text = temp.replaceAll("]", "");
						var objectArray = text.split('},');
						$('#log-list').empty();
						$.each(objectArray, function(index, element){
							if (element.indexOf('}') != -1){
								var object = JSON.parse(element);
							}else {
								var object = JSON.parse(element + '}');
							}
							
							processEntry(object, MasterTable);
						});
					});
			}
			
			
			
			
			var MasterTable = new Array();
			var childrenArray = new Array();
			
			for (var i = 0; i < data2.length; i++){
				var child = data2[i];
				var name = child["name"];
				var img = child["picture"];
				MasterTable[name] = img;
				childrenArray[i] = name;
			}
			
			//archive_sidebar
			$.each(childrenArray, function(index, element){
				var name = element;
				var option = $('<option value="' + name + '">' + name + '</option>');
				$("#select_children").append(option);
				
			});
			


			
			$("#select_children").multiselect({
//				selectedText: "Filter by Child", 
				   close: function(){
					   redoQuery(MasterTable);
					   //do the query

				   }

			});
			$("#select_type").multiselect({
				close: function(){
					redoQuery(MasterTable);
				   
				}
			});
			$("#select_children").multiselect("checkAll");
			$("#select_type").multiselect("checkAll");
			
			

			
			processEntries(data, MasterTable);
			
			
		});
		
	});
});